using System;
using System.Collections.Generic;
using System.Web.Security;
using RMH.DataAccess;
using RMH.Entities;
using RMH.Services;
using RMH.Web;

namespace RMH.Web.RMHMembership
{
    public class RMHRoleProvider : RoleProvider
    {
        private IStaffService staffService;
        private IStaffRepository staffRepository;
        private IStaffRoleRepository staffRoleRepository;

        public RMHRoleProvider(IStaffService staffService, IStaffRepository staffRepository,
            IStaffRoleRepository staffRoleRepository)
        {
            this.staffService = staffService;
            this.staffRepository = staffRepository;
            this.staffRoleRepository = staffRoleRepository;
        }

        public RMHRoleProvider()
        {
            staffService = Container.Resolve<IStaffService>();
            staffRepository = Container.Resolve<IStaffRepository>();
            staffRoleRepository = Container.Resolve<IStaffRoleRepository>();            
        }

        public override bool IsUserInRole(string username, string roleName)
        {            
            var staff = staffService.GetByUserName(username);
            if (staff == null || staff.Roles == null) return false;
            foreach (var role in staff.Roles)
                if (role.Name == roleName)
                    return true;
            return false;
        }

        public override string[] GetRolesForUser(string username)
        {
            var roleList = new List<string>();
            var user = staffService.GetByUserName(username);
            
            if (user == null) return roleList.ToArray();

            foreach (var role in user.Roles)
            {
                roleList.Add(role.Name);
            }

            return roleList.ToArray();

        }

        public override void CreateRole(string roleName)
        {
            var role = new StaffRole {Name = roleName};
            staffRoleRepository.Save(role);
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            var role = staffRoleRepository.GetByName(roleName);
            return role != null && role.Id > 0;
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            
            foreach (var username in usernames)
            {
                var user = staffService.GetByUserName(username);
                if (user == null)
                    throw new ArgumentException("User not found: " + username);
                foreach (var roleName in roleNames)
                {
                    
                    bool hasRole = false;
                    foreach (var role in user.Roles)
                    {
                        if (role.Name == roleName)
                            hasRole = true;
                    }

                    if (!hasRole)
                    {
                        user.Roles.Add(staffRoleRepository.GetByName(roleName));
                    }

                }
                staffService.Save(user);                
            }            
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }
    }
}